package com.moon.admin.tools.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.apache.commons.lang3.StringUtils;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

import com.moon.admin.framework.util.MoonException;
import com.moon.admin.tools.ToolsConstants;
import com.moon.admin.tools.entity.GenTable;

/**
 * 模板处理工具类
 *
 * @author moon
 */
public class VelocityUtils {
    /**
     * 项目空间路径
     */
    private static final String PROJECT_PATH = "main/java";


    /**
     * 默认上级菜单，系统工具
     */
    private static final String DEFAULT_PARENT_MENU_ID = "0";

    /**
     * 初始化vm方法
     */
    public static void initVelocity() {
        Properties p = new Properties();
        try {
            // 加载classpath目录下的vm文件
            p.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
            // 定义字符集
            p.setProperty(Velocity.ENCODING_DEFAULT, "UTF-8");
            p.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");
            // 初始化Velocity引擎，指定配置Properties
            Velocity.init(p);
        } catch (Exception e) {
            throw new MoonException(e);
        }
    }

    /**
     * 设置模板变量信息
     *
     * @return 模板列表
     */
    public static VelocityContext prepareContext(GenTable genTable) {
        String className = genTable.getClassName().substring(0, 1).toLowerCase() + genTable.getClassName().substring(1);
        String tplCategory = genTable.getTplCategory();

        VelocityContext velocityContext = new VelocityContext();
        velocityContext.put("tplCategory", genTable.getTplCategory());
        velocityContext.put("tableName", genTable.getTableName());
        velocityContext.put("ClassName", genTable.getClassName());
        velocityContext.put("classname", className);
        velocityContext.put("parentMenuId", genTable.getParentMenuId());
        velocityContext.put("moduleName", genTable.getModuleName());
        velocityContext.put("package", genTable.getPackageName());
        velocityContext.put("tableComment", StringUtils.isNotEmpty(genTable.getTableComment()) ? genTable.getTableComment() : "【请填写功能名称】");
        velocityContext.put("author", genTable.getFunctionAuthor());
        velocityContext.put("columns", genTable.getColumns());
        if (ToolsConstants.TPL_TREE.equals(tplCategory)) {
//            setMenuVelocityContext(velocityContext, genTable);
        }
        return velocityContext;
    }

//    public static void setMenuVelocityContext(VelocityContext context, GenTable genTable)
//    {
//        String options = genTable.getOptions();
//        JsonNode paramsObj = JsonUtils.str2node(options);
//        String parentMenuId = getParentMenuId(paramsObj);
//        context.put(GenConstants.PARENT_MENU_ID, parentMenuId);
//    }


    /**
     * 获取模板信息
     *
     * @return 模板列表
     */
    public static List<String> getTemplateList(String tplCategory) {
        List<String> templates = new ArrayList<String>();
        templates.add("vm/java/domain.java.vm");
        templates.add("vm/java/entity.java.vm");
        templates.add("vm/java/mapper.java.vm");
        templates.add("vm/java/service.java.vm");
        templates.add("vm/java/controller.java.vm");
        templates.add("vm/sql/sql.sql.vm");
        templates.add("vm/vue/index.js.vm");
        if (ToolsConstants.TPL_CRUD.equals(tplCategory)) {
            templates.add("vm/vue/index.vue.vm");
        } else if (ToolsConstants.TPL_TREE.equals(tplCategory)) {
            templates.add("vm/vue/index-tree.vue.vm");
        }
        return templates;
    }

    /**
     * 获取文件名
     */
    public static String getFileName(String template, GenTable genTable) {
        // 文件名称
        String fileName = "";
        // 包路径
        String packageName = genTable.getPackageName();
        // 模块名
        String moduleName = genTable.getModuleName();
        // 大写类名
        String className = genTable.getClassName();
        // 大写类名
        String classname = genTable.getClassName().substring(0, 1).toLowerCase() + genTable.getClassName().substring(1);

        String javaPath = PROJECT_PATH + "/" + StringUtils.replace(packageName, ".", "/");
        String vuePath = "vue";

        if (template.contains("domain.java.vm")) {
            fileName = String.format("%s/domain/%sQuery.java", javaPath, className);
        } else if (template.contains("entity.java.vm")) {
            fileName = String.format("%s/entity/%sEntity.java", javaPath, className);
        } else if (template.contains("mapper.java.vm")) {
            fileName = String.format("%s/mapper/%sMapper.java", javaPath, className);
        } else if (template.contains("service.java.vm")) {
            fileName = String.format("%s/service/%sService.java", javaPath, className);
        } else if (template.contains("controller.java.vm")) {
            fileName = String.format("%s/controller/%sController.java", javaPath, className);
        } else if (template.contains("sql.vm")) {
            fileName = className + "Menu.sql";
        } else if (template.contains("index.js.vm")) {
            fileName = String.format("%s/views/%s/js/%s.js", vuePath, moduleName, classname);
        } else if (template.contains("index.vue.vm")) {
            fileName = String.format("%s/views/%s/%s.vue", vuePath, moduleName, classname);
        } else if (template.contains("index-tree.vue.vm")) {
            fileName = String.format("%s/views/%s/%s.vue", vuePath, moduleName, classname);
        }
        return fileName;
    }
}